//     @Version: V2.1
//
//     07/17/08 PGV 2.1.001.0 Starting V2.1 (based on 2.0.030.0 code)

using System;
using System.IO;
using System.Data.SqlClient;
using System.Diagnostics;

namespace H2V2
{
	public class LogFile
	{
        public string m_FileName;
		private FileStream m_file;
		private StreamWriter m_stream;
		public bool DupToConsole = false;
		bool EmergencyWriteShutOff = false; 

		public LogFile (bool a_dupToConsole)
		{
			DupToConsole = a_dupToConsole;
			m_FileName = "H2log.txt";

			try
			{
				CheckFileSize ();
				m_file = new FileStream (m_FileName, FileMode.Append, FileAccess.Write);
				m_stream = new StreamWriter (m_file);
				m_stream.AutoFlush = true; 
			}
			catch (Exception ex)
			{
				Console.WriteLine ("Unable to initialize the log file");
				throw ex;
			}

			try
			{
				if (m_file.Length > 0) Write ("\t ");
				Write ("\tOpening Log ");
			}
			catch (Exception ex)
			{
				Console.WriteLine ("Unable to write to the log file.");
				throw ex;
			}

			Console.WriteLine ("Run details are recorded into H2log.txt log file");

			TextWriterTraceListener tl = new TextWriterTraceListener (m_file);
			Debug.Listeners.Add (tl);
			Debug.AutoFlush = true;
		}

		void CheckFileSize ()
		{
			string stamp,
				r_name;
			
			if (File.Exists (m_FileName))
			{
				FileInfo fi = new FileInfo (m_FileName);
				if (fi.Length > 102400)
				{
					stamp = DateTime.Now.ToString ("MMddyyHHmmss");
					r_name = "H2Log_"+stamp+".txt";

					// Attempt just once; If it fails - will attempt next time
					try
					{
						fi.MoveTo (r_name);
					}
					catch {}
				}
			}
		}

		public void Flush ()
		{
			m_stream.Flush ();
		}

		public void Close ()
		{
			try
			{
				Write ("\tClosing Log ");
				m_stream.WriteLine ("\t\t ");
				m_stream.Flush ();
				m_stream.Close (); 
			}
			catch (ObjectDisposedException)
			{
			}
			catch (IOException)
			{
			}
		}

		void BaseWrite (string a_format, params object[] args)
		{
			try
			{
				if (DupToConsole) Console.WriteLine (a_format, args);
				if (!EmergencyWriteShutOff) 
				{
					m_stream.WriteLine (a_format, args);
				}
			}
			catch (System.IO.IOException ioex)
			{
				string msg = ioex.Message;
				EmergencyWriteShutOff = true;	// don't try to write anymore
				throw;
			}
		}

		public void Write (string a_input) 
		{
			BaseWrite ("{0} \t{1}", DateTime.Now.ToString ("G"), a_input);
		}

		public void WriteDebug (string a_input)
		{
			try
			{
				if (!EmergencyWriteShutOff) 
				{
					Debug.WriteLine ("\tDEBUG: \t"+a_input);
				}
			}
			catch (System.IO.IOException ioex)
			{
				string msg = ioex.Message;
				EmergencyWriteShutOff = true;	// don't try to write anymore
				throw;
			}
		}

		public void WriteStack (string a_input)
		{
			try
			{
				if (!EmergencyWriteShutOff) 
				{
					Debug.WriteLine (a_input);
				}
			}
			catch (System.IO.IOException ioex)
			{
				string msg = ioex.Message;
				EmergencyWriteShutOff = true;	// don't try to write anymore
				throw;
			}
		}

		public void SQLException (SqlException sqlex)
		{
			Write ("ERROR: \t"+sqlex.Number+" sev "+sqlex.Class+" "+sqlex.Message);

			foreach (SqlError Error in sqlex.Errors)
			{
				if (Error.Number != sqlex.Number && Error.Message != sqlex.Message)
				{
					Write ("ERROR: \t"+Error.Number+" sev "+Error.Class+" "+Error.Message);
				}
			}
		}
	}
}
